<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// ------------------------------------------------------------------------

/**
 * Logging Class
 *
 * @package		XJPHP
 * @subpackage	Libraries
 * @category	Logging
 */
class XJ_Log {

	protected $_log_path;
	protected $_threshold	= 1;
	protected $_date_fmt	= 'Y-m-d H:i:s';
	protected $_enabled	= TRUE;
	protected $_levels	= array('ERROR' => '2', 'DEBUG' => '0',  'INFO' => '1', 'ALL' => '3');

	/**
	 * Constructor
	 */
	public function __construct()
	{
		$config =& xj_get_config();
		if (isset($config['log_path']) and $config['log_path'] != ''){
			$this->_log_path = $config['log_path'];
		}
		$app_dir_str = '';
		if (isset($_ENV['XJ_APP_DIR_PATH_ARR']) && is_array($_ENV['XJ_APP_DIR_PATH_ARR'])){
			$app_dir_arr = $_ENV['XJ_APP_DIR_PATH_ARR'];//array_reverse($_ENV['XJ_APP_DIR_PATH_ARR']);
			foreach ($app_dir_arr as $app_dir){
				$file_path = $app_dir.'logs'.DIRECTORY_SEPARATOR;
				$app_dir_str = $app_dir_str."\n[".$file_path."]";
				if (file_exists($file_path))
				{
					$this->_log_path = $file_path;
				}
			}
		}else {
			$this->_log_path = $_ENV['XJ_APPPATH'].'logs'.DIRECTORY_SEPARATOR;
		}

		if ( ! is_dir($this->_log_path) OR ! xj_is_really_writable($this->_log_path))
		{
			//xj_sys_show_error($app_dir_str);
			$this->_enabled = FALSE;
		}

		if (isset($config['log_threshold']) and is_numeric($config['log_threshold']))
		{
			$this->_threshold = $config['log_threshold'];
		}

		if (isset($config['log_date_format']) and $config['log_date_format'] != '')
		{
			$this->_date_fmt = $config['log_date_format'];
		}
	}

	// --------------------------------------------------------------------

	/**
	 * Write Log File
	 *
	 * Generally this function will be called using the global log_message() function
	 *
	 * @param	string	the error level
	 * @param	string	the error message
	 * @param	bool	whether the error is a native PHP error
	 * @return	bool
	 */
	public function write_log($level = 'error', $msg, $php_error = FALSE)
	{
		if ($this->_enabled === FALSE)
		{
			//xj_sys_show_error("_enabled");
			return FALSE;
		}

		$level = strtoupper($level);

		if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] < $this->_threshold))
		{
			//xj_sys_show_error("_levels");
			return FALSE;
		}

		$filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
		$message  = '';

		if ( ! file_exists($filepath))
		{
			$message .= "<"."?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
		}

		if ( ! $fp = @fopen($filepath,  'ab'))
		{
			//xj_sys_show_error("fopen:".$filepath);
			return FALSE;
		}

		$message .= '['.date($this->_date_fmt). ']'.'['.$level.''.(($level == 'INFO') ? ' ]' : ']').$msg;

		flock($fp, LOCK_EX);
		fwrite($fp, $message, strlen($message));
		fwrite($fp, "\n", 1);
		flock($fp, LOCK_UN);
		fclose($fp);

		@chmod($filepath, FILE_WRITE_MODE);
		return TRUE;
	}

}
// END Log Class

/* End of file Log.php */
/* Location: ./xj_core/libraries/Log.php */